home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / libpcap-0.0.6 / pcap-dlpi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-25  |  11.3 KB  |  510 lines

  1. /*
  2.  * Copyright (c) 1993, 1994, 1995
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk),
  22.  * University College London.
  23.  */
  24. #ifndef lint
  25. static  char rcsid[] =
  26.     "@(#)$Header: pcap-dlpi.c,v 1.22+ 94/10/12 20:08:15 leres Exp $ (LBL)";
  27. #endif
  28.  
  29. /*
  30.  * Packet capture routine for dlpi under SunOS 5
  31.  *
  32.  * Notes:
  33.  *
  34.  *    - Apparently the DLIOCRAW ioctl() is specific to SunOS.
  35.  *
  36.  *    - There is a bug in bufmod(7) such that setting the snapshot
  37.  *      length results in data being left of the front of the packet.
  38.  *
  39.  *    - It might be desirable to use pfmod(7) to filter packets in the
  40.  *      kernel.
  41.  */
  42.  
  43. #include <sys/types.h>
  44. #include <sys/time.h>
  45. #include <sys/bufmod.h>
  46. #include <sys/dlpi.h>
  47. #include <sys/stream.h>
  48. #include <sys/systeminfo.h>
  49.  
  50. #include <net/bpf.h>
  51.  
  52. #include <ctype.h>
  53. #include <errno.h>
  54. #include <fcntl.h>
  55. #include <memory.h>
  56. #include <signal.h>
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <stropts.h>
  61. #include <unistd.h>
  62.  
  63. #include "pcap-int.h"
  64.  
  65. #define    MAXDLBUF    8192
  66.  
  67. /* Forwards */
  68. static int send_request(int, char *, int, char *, char *);
  69. static int dlattachreq(int, u_long, char *);
  70. static int dlinfoack(int, char *, char *);
  71. static int dlinforeq(int, char *);
  72. static int dlpromisconreq(int, u_long, char *);
  73. static int dlokack(int, char *, char *);
  74. static int strioctl(int, int, int, char *);
  75. #ifdef SOLARIS
  76. static char *getrelease(long *, long *, long *);
  77. #endif
  78.  
  79. int
  80. pcap_stats(pcap_t *p, struct pcap_stat *ps)
  81. {
  82.  
  83.     *ps = p->md.stat;
  84.     return (0);
  85. }
  86.  
  87. int
  88. pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  89. {
  90.     register int cc, n;
  91.     register u_char *bp, *ep, *pk;
  92.     register struct bpf_insn *fcode;
  93.     register struct sb_hdr *sbp;
  94.     int flags;
  95.     struct strbuf data;
  96.     struct pcap_pkthdr pkthdr;
  97.  
  98.     flags = 0;
  99.     cc = p->cc;
  100.     if (cc == 0) {
  101.         data.buf = (char *)p->buffer;
  102.         data.maxlen = MAXDLBUF;
  103.         data.len = 0;
  104.         do {
  105.             if (getmsg(p->fd, NULL, &data, &flags) < 0) {
  106.                 /* Don't choke when we get ptraced */
  107.                 if (errno == EINTR) {
  108.                     cc = 0;
  109.                     continue;
  110.                 }
  111.                 strcpy(p->errbuf, pcap_strerror(errno));
  112.                 return (-1);
  113.             }
  114.             cc = data.len;
  115.         } while (cc == 0);
  116.         bp = p->buffer;
  117.     } else
  118.         bp = p->bp;
  119.  
  120.     /* Loop through packets */
  121.     fcode = p->fcode.bf_insns;
  122.     ep = bp + cc;
  123.     n = 0;
  124.     while (bp < ep) {
  125.         sbp = (struct sb_hdr *)bp;
  126.         p->md.stat.ps_drop += sbp->sbh_drops;
  127.         ++p->md.stat.ps_recv;
  128.         pk = bp + sizeof(*sbp);
  129.         bp += sbp->sbh_totlen;
  130.         if (bpf_filter(fcode, pk, sbp->sbh_origlen, sbp->sbh_msglen)) {
  131.             pkthdr.ts = sbp->sbh_timestamp;
  132.             pkthdr.len = sbp->sbh_origlen;
  133.             pkthdr.caplen = sbp->sbh_msglen;
  134.             /* Insure caplen does not exceed snapshot */
  135.             if (pkthdr.caplen > p->snapshot)
  136.                 pkthdr.caplen = p->snapshot;
  137.             (*callback)(user, &pkthdr, pk);
  138.             if (++n >= cnt && cnt >= 0) {
  139.                 p->cc = ep - bp;
  140.                 p->bp = bp;
  141.                 return (n);
  142.             }
  143.         }
  144.     }
  145.     p->cc = 0;
  146.     return (n);
  147. }
  148.  
  149. pcap_t *
  150. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  151. {
  152.     register pcap_t *p;
  153.     long    buf[MAXDLBUF];
  154.     int ppa;
  155.     int cppa;
  156.     register dl_info_ack_t *infop;
  157.     u_long ss, flag;
  158. #ifdef SOLARIS
  159.     char *release;
  160.     long osmajor, osminor, osmicro;
  161. #endif
  162.     char dname[100];
  163.  
  164.     p = (pcap_t *)malloc(sizeof(*p));
  165.     if (p == NULL) {
  166.         strcpy(ebuf, pcap_strerror(errno));
  167.         return (NULL);
  168.     }
  169.     memset(p, 0, sizeof(*p));
  170.  
  171.     /*
  172.     ** 1) In order to get the ppa take the last character of the device
  173.     ** name if it is a number then fail the open.
  174.     **
  175.     ** 2) If the name starts with a '/' then this is an absolute pathname,
  176.     ** otherwise prepend '/dev/'.
  177.     **
  178.     ** 3) Remove the trailing digit and try and open the device
  179.     ** not staggeringly intuitive but it should work.
  180.     **
  181.     ** If there are more than 9 devices this code will fail.
  182.     */
  183.  
  184.     cppa = device[strlen(device) - 1];
  185.     if (!isdigit(cppa)) {
  186.         sprintf(ebuf, "%c is not a digit, therefore not a valid ppa",
  187.             cppa);
  188.         goto bad;
  189.     }
  190.  
  191.     dname[0] = '\0';
  192.     if (device[0] != '/')
  193.         strcpy(dname, "/dev/");
  194.  
  195.     strcat(dname, device);
  196.     dname[strlen(dname) - 1] = '\0';
  197.  
  198.     if ((p->fd = open(dname, O_RDWR)) < 0) {
  199.         sprintf(ebuf, "%s: %s", dname, pcap_strerror(errno));
  200.         goto bad;
  201.     }
  202.     p->snapshot = snaplen;
  203.  
  204.     ppa = cppa - '0';
  205.     /*
  206.     ** Attach.
  207.     */
  208.     if (dlattachreq(p->fd, ppa, ebuf) < 0 ||
  209.         dlokack(p->fd, (char *)buf, ebuf) < 0)
  210.         goto bad;
  211.  
  212.     if (promisc) {
  213.         /*
  214.         ** enable promiscuous.
  215.         */
  216.         if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 ||
  217.             dlokack(p->fd, (char *)buf, ebuf) < 0)
  218.             goto bad;
  219.         if (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 ||
  220.             dlokack(p->fd, (char *)buf, ebuf) < 0)
  221.             goto bad;
  222.  
  223.         /*
  224.         ** enable multicast, you would have thought promiscuous
  225.         ** would be sufficient.
  226.         */
  227.         if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 ||
  228.             dlokack(p->fd, (char *)buf, ebuf) < 0)
  229.             goto bad;
  230.     }
  231.  
  232.     /*
  233.     ** Determine link type
  234.     */
  235.     if (dlinforeq(p->fd, ebuf) < 0 ||
  236.         dlinfoack(p->fd, (char *)buf, ebuf) < 0)
  237.         goto bad;
  238.  
  239.     infop = &((union DL_primitives *)buf)->info_ack;
  240.     switch (infop->dl_mac_type) {
  241.  
  242.     case DL_ETHER:
  243.         p->linktype = DLT_EN10MB;
  244.         break;
  245.  
  246.     case DL_FDDI:
  247.         p->linktype = DLT_FDDI;
  248.         break;
  249.  
  250.     default:
  251.         sprintf(ebuf, "unknown mac type 0x%lu", infop->dl_mac_type);
  252.         goto bad;
  253.     }
  254.  
  255. #ifdef    DLIOCRAW
  256.     /*
  257.     ** This is a non standard SunOS hack to get the ethernet header.
  258.     */
  259.     if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
  260.         sprintf(ebuf, "DLIOCRAW: %s", pcap_strerror(errno));
  261.         goto bad;
  262.     }
  263. #endif
  264.  
  265.     /*
  266.     ** Another non standard call to get the data nicely buffered
  267.     */
  268.     if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {
  269.         sprintf(ebuf, "I_PUSH bufmod: %s", pcap_strerror(errno));
  270.         goto bad;
  271.     }
  272.  
  273.     /*
  274.     ** Now that the bufmod is pushed lets configure it.
  275.     **
  276.     ** There is a bug in bufmod(7). When dealing with messages of
  277.     ** less than snaplen size it strips data from the beginning not
  278.     ** the end.
  279.     **
  280.     ** This bug is supposed to be fixed in 5.3.2. Also, there is a
  281.     ** patch available. Ask for bugid 1149065.
  282.     */
  283.     ss = snaplen;
  284. #ifdef SOLARIS
  285.     release = getrelease(&osmajor, &osminor, &osmicro);
  286.     if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
  287.         getenv("BUFMOD_FIXED") == NULL) {
  288.         fprintf(stderr,
  289.         "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n",
  290.             release);
  291.         ss = 0;
  292.     }
  293. #endif
  294.     if (ss > 0 &&
  295.         strioctl(p->fd, SBIOCSSNAP, sizeof(u_long), (char *)&ss) != 0) {
  296.         sprintf(ebuf, "SBIOCSSNAP: %s", pcap_strerror(errno));
  297.         goto bad;
  298.     }
  299.  
  300.     /*
  301.     ** Set up the bufmod flags
  302.     */
  303.     if (strioctl(p->fd, SBIOCGFLAGS, sizeof(u_long), (char *)&flag) < 0) {
  304.         sprintf(ebuf, "SBIOCGFLAGS: %s", pcap_strerror(errno));
  305.         goto bad;
  306.     }
  307.     flag |= SB_NO_DROPS;
  308.     if (strioctl(p->fd, SBIOCSFLAGS, sizeof(u_long), (char *)&flag) != 0) {
  309.         sprintf(ebuf, "SBIOCSFLAGS: %s", pcap_strerror(errno));
  310.         goto bad;
  311.     }
  312.     /*
  313.     ** Set up the bufmod timeout
  314.     */
  315.     if (to_ms != 0) {
  316.         struct timeval to;
  317.  
  318.         to.tv_sec = to_ms / 1000;
  319.         to.tv_usec = (to_ms * 1000) % 1000000;
  320.         if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
  321.             sprintf(ebuf, "SBIOCSTIME: %s", pcap_strerror(errno));
  322.             goto bad;
  323.         }
  324.     }
  325.  
  326.     /*
  327.     ** As the last operation flush the read side.
  328.     */
  329.     if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
  330.         sprintf(ebuf, "FLUSHR: %s", pcap_strerror(errno));
  331.         goto bad;
  332.     }
  333.     /* Allocate data buffer */
  334.     p->bufsize = MAXDLBUF * sizeof(long);
  335.     p->buffer = (u_char *)malloc(p->bufsize);
  336.     return (p);
  337. bad:
  338.     free(p);
  339.     return (NULL);
  340. }
  341.  
  342. int
  343. pcap_setfilter(pcap_t *p, struct bpf_program *fp)
  344. {
  345.  
  346.     p->fcode = *fp;
  347.     return (0);
  348. }
  349.  
  350. static int
  351. send_request(int fd, char *ptr, int len, char *what, char *ebuf)
  352. {
  353.     struct    strbuf    ctl;
  354.     int    flags;
  355.  
  356.     ctl.maxlen = 0;
  357.     ctl.len = len;
  358.     ctl.buf = ptr;
  359.  
  360.     flags = 0;
  361.     if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
  362.         sprintf(ebuf, "putmsg \"%s\"failed: %s",
  363.             what, pcap_strerror(errno));
  364.         return (-1);
  365.     }
  366.     return (0);
  367. }
  368.  
  369. static int
  370. dlattachreq(int fd, u_long ppa, char *ebuf)
  371. {
  372.     dl_attach_req_t    req;
  373.  
  374.     req.dl_primitive = DL_ATTACH_REQ;
  375.     req.dl_ppa = ppa;
  376.  
  377.     return (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf));
  378. }
  379.  
  380. static int
  381. dlpromisconreq(int fd, u_long level, char *ebuf)
  382. {
  383.     dl_promiscon_req_t req;
  384.  
  385.     req.dl_primitive = DL_PROMISCON_REQ;
  386.     req.dl_level = level;
  387.  
  388.     return (send_request(fd, (char *)&req, sizeof(req), "promiscon", ebuf));
  389. }
  390.  
  391. static int
  392. dlokack(int fd, char *bufp, char *ebuf)
  393. {
  394.     union    DL_primitives    *dlp;
  395.     struct    strbuf    ctl;
  396.     int    flags;
  397.  
  398.     ctl.maxlen = MAXDLBUF;
  399.     ctl.len = 0;
  400.     ctl.buf = bufp;
  401.  
  402.     flags = 0;
  403.     if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
  404.         sprintf(ebuf, "getmsg: %s", pcap_strerror(errno));
  405.         return (-1);
  406.     }
  407.  
  408.     dlp = (union DL_primitives *) ctl.buf;
  409.  
  410.     if (dlp->dl_primitive != DL_OK_ACK) {
  411.         sprintf(ebuf, "dlokack unexpected primitive %d",
  412.             dlp->dl_primitive);
  413.         return (-1);
  414.     }
  415.  
  416.     if (ctl.len != sizeof(dl_ok_ack_t)) {
  417.         sprintf(ebuf, "dlokack incorrect size returned");
  418.         return (-1);
  419.     }
  420.     return (0);
  421. }
  422.  
  423.  
  424. static int
  425. dlinforeq(int fd, char *ebuf)
  426. {
  427.     dl_info_req_t req;
  428.  
  429.     req.dl_primitive = DL_INFO_REQ;
  430.  
  431.     return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
  432. }
  433.  
  434. static int
  435. dlinfoack(int fd, char *bufp, char *ebuf)
  436. {
  437.     union    DL_primitives    *dlp;
  438.     struct    strbuf    ctl;
  439.     int    flags;
  440.  
  441.     ctl.maxlen = MAXDLBUF;
  442.     ctl.len = 0;
  443.     ctl.buf = bufp;
  444.  
  445.     flags = 0;
  446.     if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
  447.         sprintf(ebuf, "dlinfoack: getmsg: %s", pcap_strerror(errno));
  448.         return (-1);
  449.     }
  450.  
  451.     dlp = (union DL_primitives *) ctl.buf;
  452.  
  453.     if (dlp->dl_primitive != DL_INFO_ACK) {
  454.         sprintf(ebuf, "dlinfoack: unexpected primitive %ld",
  455.             dlp->dl_primitive);
  456.         return (-1);
  457.     }
  458.  
  459.     /* Extra stuff like the broadcast address can be returned */
  460.     if (ctl.len < DL_INFO_ACK_SIZE) {
  461.         sprintf(ebuf, "dlinfoack: incorrect size returned");
  462.         return (-1);
  463.     }
  464.     return (0);
  465. }
  466.  
  467. static int
  468. strioctl(int fd, int cmd, int len, char *dp)
  469. {
  470.     struct strioctl str;
  471.     int rc;
  472.  
  473.     str.ic_cmd = cmd;
  474.     str.ic_timout = -1;
  475.     str.ic_len = len;
  476.     str.ic_dp = dp;
  477.     rc = ioctl(fd, I_STR, &str);
  478.  
  479.     if (rc < 0)
  480.         return (rc);
  481.     else
  482.         return (str.ic_len);
  483. }
  484.  
  485. #ifdef SOLARIS
  486. static char *
  487. getrelease(long *majorp, long *minorp, long *microp)
  488. {
  489.     char *cp;
  490.     static char buf[32];
  491.  
  492.     *majorp = 0;
  493.     *minorp = 0;
  494.     *microp = 0;
  495.     if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
  496.         return ("?");
  497.     cp = buf;
  498.     if (!isdigit(*cp))
  499.         return (buf);
  500.     *majorp = strtol(cp, &cp, 10);
  501.     if (*cp++ != '.')
  502.         return (buf);
  503.     *minorp =  strtol(cp, &cp, 10);
  504.     if (*cp++ != '.')
  505.         return (buf);
  506.     *microp =  strtol(cp, &cp, 10);
  507.     return (buf);
  508. }
  509. #endif
  510.